package org.shj.antlr;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.shj.antlr.parser.MathEmitter;
import org.shj.antlr.parser.MathLexer;
import org.shj.antlr.parser.MathParser;

/**
 * @author Shen Huang Jian
 * @date 2020-12-01 16:32
 */
public class AntlrDemo {

    public static void main(String[] args){
        CharStream input = CharStreams.fromString("(2+3)*5-25/5");
        MathLexer lexer=new MathLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        MathParser parser = new MathParser(tokens);

        // parse
        ParseTree tree = parser.prog();
        ParseTreeWalker walker = new ParseTreeWalker();

        //MathEmitter 是自己写的，其它类都是根据 Math.g4 生成的
        MathEmitter emitter = new MathEmitter();

        walker.walk(emitter, tree);
    }
}
